package com.cms.common.kit;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.cms.cache.CacheManager;

/**
 * StrKit.
 */
public class StrKit {
	
	private final static Pattern emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	
	
	public String subString(String str, int len, String mark) {
		if(StrKit.isBlank(str))return "";
		str = StrKit.delHTMLTag(str);// 去掉html代码

		len = len * 2;
		StringBuffer sb = new StringBuffer();
		int counter = 0;
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c < 255) {
				counter++;// 英文字符

			} else {
				counter = counter + 2;// 中文字符

			}
			if (counter > len) {
				String result = sb.toString().trim();
				result += mark;
				return result;
			}
			sb.append(c);
		}
		return sb.toString();
	}
	
	public static String getCurrentDateStr(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}
	
	
	public static String join(int[] ints,String split){
		String joins="";
		for (int i : ints) {
			joins+=i+split;
		}
		return joins.substring(0,joins.length()-1);
	}
	
	/**
	 * 首字母变小写
	 */
	public static String firstCharToLowerCase(String str) {
		Character firstChar = str.charAt(0);
		String tail = str.substring(1);
		str = Character.toLowerCase(firstChar) + tail;
		return str;
	}
	
	/**
	 * 首字母变大写
	 */
	public static String firstCharToUpperCase(String str) {
		Character firstChar = str.charAt(0);
		String tail = str.substring(1);
		str = Character.toUpperCase(firstChar) + tail;
		return str;
	}
	
	/**
	 * 字符串不为 null 而且不为  "" 时返回 true
	 */
	public static boolean notBlank(String str) {
		return str == null || "".equals(str.trim()) ? false : true;
	}
	
	public static boolean notBlank(String... strings) {
		if (strings == null)
			return false;
		for (String str : strings)
			if (str == null || "".equals(str.trim()))
				return false;
		return true;
	}
	
	public static boolean notNull(Object... paras) {
		if (paras == null)
			return false;
		for (Object obj : paras)
			if (obj == null)
				return false;
		return true;
	}
	
	/**
	 * 判断字符串是否为空
	 * @return
	 */
	public static boolean isBlank(String inputStr){
		if(inputStr==null||inputStr.equals("")||inputStr.length()==0)return true;
		return false;
	}
	
	/**
	 * 判断两个字符串是否相等
	 * @param resourceStr
	 * @param compStr
	 * @return
	 */
	public static boolean isEqual(String resourceStr,String compStr){
		if(resourceStr==null&&compStr!=null)return false;
		if(resourceStr!=null&&compStr==null)return false;
		if(resourceStr==null&&compStr==null){
			return true;
		}
		if(resourceStr.equals(compStr))return true;
		return false;
	}
	
	/**
	 * 首字母大写
	 * @param str 字符串参数
	 * @return 返回首字母大写后的字符串
	 */
	public static String UpperHeadWord(String str){
		return str.substring(0,1).toUpperCase()+str.substring(1);
	}
	
	/**
	 * 删除input字符串中的html格式
	 * 
	 * @param input
	 * @param length
	 * @return
	 */
	public static String splitAndFilterString(String input) {
		if (input == null || input.trim().equals("")) {
			return "";
		}
		// 去掉所有html元素,
		String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(
				"<[^>]*>", "");
		str = str.replaceAll("[(/>)<]", "");
		return str;
	}
	
	/**
	 * 过滤html,css,style
	* @Title: delHTMLTag
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param htmlStr
	* @param @return    设定文件
	
	* @author  Administrator
	* @return String    返回类型
	* @throws
	 */
	public static  String delHTMLTag(String htmlStr){ 
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
         
        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
        Matcher m_style=p_style.matcher(htmlStr); 
        htmlStr=m_style.replaceAll(""); //过滤style标签 
         
        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
        Matcher m_html=p_html.matcher(htmlStr); 
        htmlStr=m_html.replaceAll(""); //过滤html标签 
        
        htmlStr=htmlStr.replace("&nbsp;"," ");
        htmlStr=htmlStr.replaceAll("\t|\r|\n","");
        return htmlStr.trim(); //返回文本字符串 
    } 
	
	public static long bytes2long(byte[] b) {
		long temp = 0;
		long res = 0;
		for (int i=0;i<8;i++) {
			res <<= 8;
			temp = b[i] & 0xff;
			res |= temp;
		}
		return res;
	}
	
	public static int[] arrayToInt(String strs){
		String[] split = strs.split(",");
		int array[] = new int[split.length];  
		for(int i=0;i<split.length;i++)array[i]=Integer.parseInt(split[i]); 
		return array;
	}
	
	/**
	 * 判断是不是一个合法的电子邮件地址
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email){
		if(email == null || email.trim().length()==0) 
			return false;
	    return emailer.matcher(email).matches();
	}

}




